<template>
  <div class="mold-warehouse-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Search" plain type="primary" @click="openQueryDialog">
          查询
        </el-button>
        <el-button :icon="DocumentAdd" plain type="primary" @click="openAdd">
          新增
        </el-button>
        <el-button
          :disabled="currentRowData == undefined"
          :icon="Edit"
          plain
          type="warning"
          @click="openUpdateDialog"
        >
          修改
        </el-button>
        <el-button
          :disabled="currentRowData == undefined"
          :icon="Delete"
          plain
          type="danger"
          @click="deleteWc"
        >
          删除
        </el-button>
        <el-button :icon="RefreshRight" plain type="primary" @click="refresh">
          刷新
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      :cell-style="{ padding: '6px 0px' }"
      :data="wcs"
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="序号" type="index" width="80" />
      <el-table-column label="仓库编码" prop="whCode" />
      <el-table-column label="仓库名称" prop="whName" />
      <el-table-column label="工作中心" prop="wcName" />
      <el-table-column label="创建人" prop="createUser" />
      <el-table-column label="创建时间" prop="createTime" />
    </el-table>
    <el-pagination
      background
      :current-page="paginationParams.pageNum"
      :layout="layout"
      :page-size="paginationParams.pageSize"
      :total="paginationParams.total"
      @current-change="handlePaginationCurrentChange"
      @size-change="handlePaginationSizeChange"
    />

    <addWhDialog ref="addWhDialogRef" @success-callback="addCallback" />
    <queryDialog ref="queryDialogRef" @success-callback="queryCallback" />
    <updateWhDialog
      ref="updateWhDialogRef"
      @success-callback="updateCallback"
    />
    <deleteDialog ref="deleteDialogRef" @success-callback="deleteCallback" />
  </div>
</template>

<script setup>
  import {
    Search,
    DocumentAdd,
    RefreshRight,
    Edit,
    Delete,
  } from '@element-plus/icons-vue'
  import queryDialog from './modules/queryDialog.vue'
  import addWhDialog from './modules/addWhDialog.vue'
  import updateWhDialog from './modules/updateWhDialog.vue'
  import deleteDialog from './modules/deleteDialog.vue'
  import { getMoldWhPageApi } from '@/api/equip/mold_api'
  import { localStorageConfig } from '@/config/localstorage.config'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)

  const paginationParams = reactive({
    pageNum: 1,
    pageSize: 20,
    total: 0,
  })

  const addWhDialogRef = ref()
  const queryDialogRef = ref()
  const updateWhDialogRef = ref()
  const deleteDialogRef = ref()
  const queryConRef = ref({
    pkCorp: pkCorp,
  })
  const wcs = ref()
  const currentRowData = ref()

  const openQueryDialog = () => queryDialogRef.value.openDialog()

  const queryCallback = (value) => {
    queryConRef.value = value
    getWh()
  }

  const openAdd = () => addWhDialogRef.value.openDialog()

  const addCallback = () => getWh()

  const getWh = async () => {
    const load = $baseLoading()
    try {
      let params = {
        ...paginationParams,
        ...queryConRef.value,
      }
      const { code, msg, data } = await getMoldWhPageApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        wcs.value = data.list
        paginationParams.total = data.total
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  const handleCurrentChange = (value) => (currentRowData.value = value)

  const openUpdateDialog = () =>
    updateWhDialogRef.value.openDialog(currentRowData.value)

  const updateCallback = () => getWh()

  const deleteWc = () => deleteDialogRef.value.openDialog(currentRowData.value)

  const deleteCallback = () => getWh()

  const refresh = () => getWh()

  watch(paginationParams, () => getWh())

  onMounted(() => getWh())

  //#region pagintion 开始
  const layout = ref('total, sizes, prev, pager, next, jumper')
  const handlePaginationCurrentChange = (...args) => {
    paginationParams.pageNum = args[0] ? args[0] : 1
  }
  const handlePaginationSizeChange = (...args) => {
    paginationParams.pageSize = args[0] ? args[0] : 20
    paginationParams.pageNum = 1
  }
  //#endregion
</script>
